class_name EntityStateAnim_AdditionalEffect_OpenArea2DDetect
extends EntityStateAnim_AdditionalEffect

#region 属性
@export var detector_id:int
@export var is_clear:bool
@export var is_reset_mask:bool
@export var is_active_area:bool
@export var is_add_callback:bool
@export_flags("TargetCamp","SameCamp") var target_camp:int
@export_flags_2d_physics var extra_mask:int
@export var effects:Array[Entity_AdditionalEffect]
var detect_callable
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_additional_effect(entity_state:EntityState):
	super.init_additional_effect(entity_state)
func excute():
	super.excute()
	var reses:Array[Util.AddCallMethodTrackRes] = []
	var component:Area2DDetectComponent = entity.get_component_by_name("Area2DDetectComponent")
	if component:
		component = component as Area2DDetectComponent
		var detector:Area2DDetector = component.get_detector(detector_id)
		if detector:
			if is_clear:
				detector.clear_detect_target_dict()
			if is_reset_mask:
				var mask:int = _get_final_mask()
				detector.set_layer_mask(mask)
			if is_active_area:
				detector.set_area2d_active(true)
			if is_add_callback and not detector.on_detect_target.is_connected(on_detect_target_func):
				detect_callable = Callable(self,"on_detect_target_func")
				#detect_callable = func (target):
					#on_detect_target_func(target)
				detector.set_on_detect_target(detect_callable,true)
			detector.set_active(true)
	return reses
#endregion
#region 公共方法
func on_detect_target_func(target):
	for effect in effects:
		effect.add_effect(entity,target)
func get_detect_callable():
	return detect_callable
#endregion
#region 私有方法
func _get_final_mask():
	var mask:int = extra_mask
	var camp_component = entity.get_component_by_name("CampComponent") as CampComponent
	var targets:Array[GameEnum.Camp] = []
	if (1 << 0) & target_camp > 0:
		targets.append_array(camp_component.get_target_camps())
	if (1 << 1) & target_camp > 0:
		targets.append_array(camp_component.get_camp_camps())
	for target_camp in targets:
		var layer_mask = entity.get_entity_data().get_layer_mask_by_tag(GameEnum.Camp.keys()[target_camp])
		mask |= layer_mask
	return mask
#endregion
#region 生命周期函数
#endregion
